var users = new Array(); //存放所有用户的数组

var operateType; // 按钮的操作类型

// 创建用户的构造方法
function user(code,name,sex,birthday,email,remark){
    this.code = code;
    this.name = name;
    this.sex = sex;
    this.birthday = birthday;
    this.email = email;
    this.remark = remark;
}

// 当点击添加按钮
$("#user_add").on("click", function () {

    $("#myModalLabel").val("新增页面");
    operateType = "add";
});

//当点击修改按钮
$("#user_edit").on("click", function () {
    $("#myModalLabel").val("修改页面");
    operateType = "edit";
});

//当点击删除按钮
$("#user_delete").on("click",function (){
    var checkboxs = $(" [type='checkbox']");  // 获取所有复选框
    let f = false;  // 是否选中用户的标记
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {  // 某个复选框被选中
            f = true;  // 标记 有 复选框被选中
            box = $(box); //dom转为jq对象
            code = box.parent().siblings("#code").text();
            /*var m_code = $("#code").val();*/
            console.log(code);
            window.location.href="deleteStudentServlet?code="+code;
        }
    }
    if(!f){
        alert("请选择一个用户！");
    }
});

//添加用户
$("#save").on("click", function () {
    if (operateType == "add") {
        $.ajax({
            type: "post",
            url:  "addStudentServlet",
            data: /*JSON.stringify(user),*/
                {
                    code: $("#m_code").val(),
                    userName : $("#m_userName").val(),
                    sex : $("#m_sex").val(),
                    birthday : $("#m_birthday").val(),
                    email : $("#m_email").val(),
                    remark : $("#m_remark").val()
                },
            dataType: 'JSON',
            success: function (data, status) {
                if (status == "success") {
                   alert("增加成功!")
                }else{
                    alert("增加失败!")
                }
            }

        });


        // window.location.href="/addStudentServlet?code="+m_code+"&userName="+m_userName+"&sex="+m_sex+"&birthday="+m_birthday+"&eamil"+m_email+"&remark"+m_remark;
    } else if (operateType == "edit") {
        $.ajax({
            type: "post",
            url:  "updateStudentServlet",
            data: /*JSON.stringify(user),*/
                {
                    code: $("#m_code").val(),
                    userName : $("#m_userName").val(),
                    sex : $("#m_sex").val(),
                    birthday : $("#m_birthday").val(),
                    email : $("#m_email").val(),
                    remark : $("#m_remark").val()
                },
            dataType: 'JSON',
            success: function (data, status) {
                if (status == "success") {
                    alert("修改成功!")
                }else{
                    alert("修改失败!")
                }
            }
        });

        // window.location.href="/updateStudentServlet?code="+m_code+"&userName="+m_userName+"&sex="+m_sex+"&birthday="+m_birthday+"&eamil"+m_email+"&remark"+m_remark;
        alert("修改成功");
    }

});
// 查询用户
$("#user_find").on("click",function (){

    $.ajax({
        type: "post",
        url:  "findStudentServlet",
        data:
            {
                code: $("#id").val()
            },
        dataType: 'JSON',
        success: function (data, status) {
            if (status == "success") {
                alert("修改成功!")
            }else{
                alert("修改失败!")
            }
        }
    });
});

//编辑用户
$("#user_edit").on("click", function () {
    var checkboxs = $("input[type='checkbox']");
    let f = false;
    for (var i = 0; i < checkboxs.length; i++) {
        var box = checkboxs[i];
        if (box.checked) {
            f = true;
            $(this).attr("data-toggle","modal");  // 启用模态框
            $(this).attr("data-target","#myModal"); // 启用模态框
            box = $(box);
            code = box.parent().siblings("#code").text();
            // 获取表格中选中的用户的各项数据
            var userName = box.parent().siblings("#userName").text();
            var sex = box.parent().siblings("#sex").text();
            var birthday = box.parent().siblings("#birthday").text();
            var email = box.parent().siblings("#mail").text();
            var remark = box.parent().siblings("#remark").text();
            // 选中用户的各项数据，放在模态框的相应input框中
            $("#m_code").val(code);
            $("#m_userName").val(userName);
            $("#m_sex").val(sex);
            $("#m_birthday").val(birthday);
            $("#m_email").val(email);
            $("#m_remark").val(remark);
        }
    }
    if(!f){
        alert("请选择一个用户！");
        $(this).attr("data-toggle",null); // 禁用模态框
        $(this).attr("data-target",null); // 禁用模态框
    }
});

